let avatar = '';
 
function getIdFromUrl(v) { 
  var url = new URL(window.location.href); 
  var params = new URLSearchParams(url.search);
  return params.get(v);
}

// 使用函数 
layui.use('laydate', function () {
  var laydate = layui.laydate;
  console.log('会议时间', laydate)
  //执行一个laydate实例
  laydate.render({
    elem: '#conference_time'//指定元素
  });
});

layui.use('form', function () {
  var form = layui.form;
  // 绑定select事件
  form.on('select(selectJump)', function (data) {
    console.log('跳转链接', data.value);
    // data.value 将包含用户选择的值，这里假设它是URL

    if (data.value) {
      console.log('跳转链接', data.value);
    }
  });
});
layui.use('upload', function () {
  var upload = layui.upload;
  let jingjiang_userinfo = JSON.parse(localStorage.getItem('jingjiang_userinfo'));

  //图片上传接口
  upload.render({
    elem: '#test-upload-img',
    headers: {
      token: jingjiang_userinfo.token
    },
    url: urls + '/api/common/upload', //改成你的图片上传接口
    accept: 'images', //指定允许上传的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
    done: function (res) {
      //上传完毕回调
      console.log(res);
      avatar = res.data.url;
      $('.img_shows').html("");
      var html =
        '<div class="img-box"><img class="layui-upload-img" data-id="' + res.data.fullurl + '" src="' + res.data.fullurl + '"><span data-id="' + res.data.fullurl + '" class="del"></span></div>';
      $('.img_shows').append(html);
    },
    error: function () {
      //请求出错回调
      console.log('上传失败');
    }
  });
});

$('.btn-light').click(() => {
  window.history.back();
});
$(function () {
  //富文本编辑器初始化代码
  var E = window.wangEditor;
  var editor = new E('#div1', '#div2');

  editor.customConfig.uploadImgShowBase64 = true; // 使用 base64 保存图片
  editor.create();

  //创建replaceAll()函数,用于替换字符串中出现的所有某个字符
  String.prototype.replaceAll = function (FindText, RepText) {
    return this.replace(new RegExp(FindText, 'g'), RepText);
  };

  //发布文章按钮的点击事件，即封装数据，向后台传递
  $('#publishBtn').click(function () {
    console.info('click');
    var postData = {};
       
    // getIdFromUrl 获取url的id
    postData.brand_category_id = getIdFromUrl('id') || localStorage.getItem("brand_category_id");//品牌库分类id
    postData.region_id = '';//地区分类id

    postData.title = $('#titleInput').val();
    postData.avatar = avatar;
    postData.sponsor = $("#sponsor").val();
    postData.unit = $("#unitId").val();
    postData.author = $("#authorId").val();
    postData.source = $("#sourceId").val();
    //替换双引号为两个单引号，方便编写SQL语句插入数据库中
    var content = editor.txt.html().replaceAll('"', "''");
    postData.content = content;
    postData.conference_time = $("#conference_time").val();
    // postData.type = $("#TypeID").val();
    postData.address = $("#address").val();

    postData.province_id = '';
    postData.city_id = '';
    postData.area_id = '';

    console.log('===1111=============', $("#conference_time").val());
    console.info(content);
    console.info('==================>>>>', postData);
    let typesMeeting = $("#typesMeeting").val() || '1';
    if (typesMeeting == '1') {
      createContent(postData);
    } else if (typesMeeting == '2') {
      exhibitionContent(postData);
    } else if (typesMeeting == '3') {
      createNewsContent(postData);
    } else if (typesMeeting == '4') {
      createCompanyContent(postData);
    } else if (typesMeeting == '5') {
      createArticleContent(postData);
    }
     

  });
});
//   发布会议
function createContent(data) {

  var loadIndex = layer.load(2);
  let datas = {
    url: '/api/user.user_conference/create',
    http_type: 'POST',
    datas: data
  };
  wxx.HttpReqeust(datas, function (response) {
    layer.close(loadIndex);
    if (response.code == 1) {
      layer.alert(response.msg, { title: '添加成功' });
      window.history.back();

    } else {
      layer.alert(response.msg, { title: '温馨提示' });
    }
  });
}

 
 
//   发布展会会议
function exhibitionContent(data) {
  data.exhibition_time = data.conference_time;

  var loadIndex = layer.load(2);
  let datas = {
    url: '/api/user.user_exhibition/create',
    http_type: 'POST',
    datas: data
  };
  wxx.HttpReqeust(datas, function (response) {
    layer.close(loadIndex);
    if (response.code == 1) {
      layer.alert(response.msg, { title: '添加成功' });
      window.history.back();

    } else {
      layer.alert(response.msg, { title: '温馨提示' });
    }
  });
}

//   发布新闻
function createNewsContent(data) {

  let meetingData = {
    brand_category_id: data.brand_category_id,
    region_id: data.region_id,
    title: data.title,
    avatar: data.avatar,
    content: data.content,
  }
  var loadIndex = layer.load(2);
  let datas = {
    url: '/api/user.user_news/create',
    http_type: 'POST',
    datas: meetingData
  };
  wxx.HttpReqeust(datas, function (response) {
    layer.close(loadIndex);
    if (response.code == 1) {
      layer.alert(response.msg, { title: '添加成功' });
      window.history.back();

    } else {
      layer.alert(response.msg, { title: '温馨提示' });
    }
  });
}

//   发布企业
function createCompanyContent(data) {

  let meetingData = {
    brand_category_id: data.brand_category_id,
    region_id: data.region_id,
    title: data.title,
    avatar: data.avatar,
    address: data.address,
  }
  var loadIndex = layer.load(2);
  let datas = {
    url: '/api/user.user_company/create',
    http_type: 'POST',
    datas: meetingData
  };
  wxx.HttpReqeust(datas, function (response) {
    layer.close(loadIndex);
    if (response.code == 1) {
      layer.alert(response.msg, { title: '添加成功' });
      window.history.back();

    } else {
      layer.alert(response.msg, { title: '温馨提示' });
    }
  });
} 
//   发布文章
function createArticleContent(data) {

  let meetingData = {
    brand_category_id: data.brand_category_id,
    region_id: data.region_id,
    title: data.title,
    avatar: data.avatar,
    content: data.content,
    author: data.author,
    source: data.source,
  } 
  var loadIndex = layer.load(2);
  let datas = {
    url: '/api/user.user_article/create',
    http_type: 'POST',
    datas: meetingData
  };
  wxx.HttpReqeust(datas, function (response) {
    layer.close(loadIndex);
    if (response.code == 1) {
      layer.alert(response.msg, { title: '添加成功' });
      window.history.back();

    } else {
      layer.alert(response.msg, { title: '温馨提示' });
    }
  });
}
$(".isHideAddress").hide();
$(document).ready(function () {
  $("#typesMeeting").change(function () {
    let Meeting = $("#typesMeeting").val();
    console.log('=====', Meeting)
    if (Meeting == '3' ||Meeting == '5') {
      $(".isHide").hide();
      return;
    }
    if (Meeting == '4') {
      $(".isHide").hide();
      $(".div0").hide();
      $(".isHideAddress").show();

    } else {
      $(".isHide").show();
      $(".div0").show();
      $(".isHideAddress").hide();

    }
  })
})